#include "queue.h"

typedef int BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;

BTNode* BuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		return NULL;
	}

	node->data = x;
	node->left = NULL;
	node->right = NULL;

	return node;
}

BTNode* CreatTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);
	BTNode* node7 = BuyNode(7);


	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;
	node3->right = node7;

	return node1;
}

void LevelOrder(BTNode* root)
{
	Queue q;
	QInit(&q);
	if (root)
	{
		QPush(&q, root);
	}

	while (!QEmpty(&q))
	{
		BTNode* front = QFront(&q);
		QPop(&q);
		printf("%d ", front->data);
		if(front->left)
			QPush(&q, front->left);
		if(front->right)
			QPush(&q, front->right);
	}

	QDestroy(&q);
}
bool complete(BTNode* root)
{
	Queue q;
	QInit(&q);
	if (root)
	{
		QPush(&q, root);
	}

	while (!QEmpty(&q))
	{
		BTNode* front = QFront(&q);
		QPop(&q);

		if (front == NULL)
		{
			break;
		}
		else
		{
			QPush(&q, front->left);
			QPush(&q, front->right);
		}
	}
	while (!QEmpty(&q))
	{
		BTNode* front = QFront(&q);
		QPop(&q);
		if (front != NULL)
		{
			QDestroy(&q);
			return false;
		}
	}
	QDestroy(&q);
	return true;
}

int main()
{
	BTNode* root = CreatTree();
	LevelOrder(root);
	complete(root);
	return 0;
}